﻿#pragma once


#ifndef ARD_MODULE_H
#define ARD_MODULE_H

#include <atomic>
#include <utils/enum.h>
#include <string>
#include <iostream>
#include <core/AsyncFuture.h>
#include <thread>

struct RegisiterUpdate;
// 模块基类
// 其它实际模块应私有继承该模块,基类属性被继承为派生类的私有属性
class Module
{

public:
	Module(const std::string& name, SyncType type = SyncType::Async,bool state = false);
	//回调函数用于通知寄存器变化
	virtual void callback(const RegisiterUpdate& regisiterUpdate) = 0;
	//模块自己的异步函数
	virtual AsyncFuture exec() = 0;

	void init();
	void exit();

	bool getState();
	void setState(bool state);
	SyncType getType();
	std::string getName();
	int getId();

	// 重载 << 运算符
	friend std::ostream& operator<<(std::ostream& os, const Module& obj);
private:
	static int current;
	static int _generateId;
	std::atomic_bool _state = false;
	SyncType _type = SyncType::Async;
	std::string _name;
	int _id;
};


class ExamModule : private Module
{
public:
	ExamModule(const std::string& name, SyncType type = SyncType::Async, bool state = false);
	~ExamModule();
	AsyncFuture exec() override {
		using namespace std::chrono_literals;
		std::this_thread::sleep_for(1s);
		std::cout << "create exam!\n";
		while (true)
		{
			using namespace std::chrono_literals;
			std::this_thread::sleep_for(1s);
			std::cout << "Hello World!\n";
			// 暂停协程并返回当前行,需要被显示的指定恢复运行co_yield
			co_yield true;
		}
		co_return true;// 返回字符串，并暂停协程
	};
	void callback(const RegisiterUpdate& regisiterUpdate)override {};
	void init() {
		this->Module::init();
	};
	void exit() {
		this->Module::init();
	};
private:

};


#endif // !ARD_MODULE_H



